//
//  BIZMyTableViewController.m
//  ConsultantVersion
//
//  Created by 李小龙 on 2017/3/24.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZMyTableViewController.h"
#import "BIZUpdatePwdVC.h"
@interface BIZMyTableViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *logoImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *posiationLabel;
@property (weak, nonatomic) IBOutlet UILabel *officeNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *gradeLabel;

@property (weak, nonatomic) IBOutlet UILabel *onlineCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *repaidCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *offlineCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *contraBIZountLabel;

@end

@implementation BIZMyTableViewController
- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self loadUeserInfoData];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.automaticallyAdjustsScrollViewInsets = NO;
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setTitleTextAttributes:@{NSFontAttributeName:SYSTEMFONT(17),NSForegroundColorAttributeName:[UIColor clearColor]}];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //去除导航条透明后导航条下的黑线
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}


-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.automaticallyAdjustsScrollViewInsets = YES;
    //    self.automaticallyAdjustsScrollViewInsets = YES;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:COLOR_NAVBAR_BLUE]
                                                  forBarMetrics:UIBarMetricsDefault];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            BIZUpdatePwdVC *vc = [[BIZUpdatePwdVC alloc]init];
            BIZNavigationController *nav = [[BIZNavigationController alloc]initWithRootViewController:vc];
            vc.updatePwdSuccessReturnBlock = ^(){
                MPWeakSelf(self);
                [weakself loadUeserInfoData];
            };
            
            [self presentViewController:nav animated:YES completion:nil];
        }
        else {
            [MBProgressHUD showAnimationHUD];
            [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
                [MBProgressHUD showSuccessMessage:@"清理完成"];
                [self setCacheSize];
            }];
        }
        
        
    }

    else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定退出吗？" preferredStyle:UIAlertControllerStyleAlert];
       
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:cancel];
        
        
        UIAlertAction *confim = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self loginOut];
        }];
        [alert addAction:confim];
        
        
        [self presentViewController:alert animated:YES completion:nil];
    }
}


- (void)setCacheSize
{
    //    NSString *fullPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"CustomFile"];
    //
    //    unsigned long long fsize = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil].fileSize;
    //    DeLog(@"fileSize %llu", fsize);
    
    NSInteger size = [SDImageCache sharedImageCache].getSize;
    NSString *sizeText = nil;
    if (size >= pow(10, 9)) {
        sizeText = [NSString stringWithFormat:@"%.2fGB", size / pow(10, 9)];
    } else if (size >= pow(10, 6)) {
        sizeText = [NSString stringWithFormat:@"%.2fMB", size / pow(10, 6)];
    } else if (size >= pow(10, 3)) {
        sizeText = [NSString stringWithFormat:@"%.2fKB", size / pow(10, 3)];
    } else {
        sizeText = [NSString stringWithFormat:@"%zdB", size];
    }
//    self.cacheSize.text = sizeText;
}


- (void)loginOut
{
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:kLoginOutUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DeLog(@"responseObject:++++%@",responseObject);
        
        if (([responseObject[@"code"] integerValue] == 1) ||([responseObject[@"code"] integerValue]  == 2)) {
            [BIZUserModel shareUser].cookieID = nil;
            [BIZUserModel shareUser].login = NO;
            [MBProgressHUD showInfoMessage:responseObject[@"des"]];
            
            BIZLoginViewController *vc = [BIZLoginViewController viewControllerFromStoryboard];
            MPWeakSelf(self);
            vc.loginSuccessBlock = ^(){
                [weakself loadUeserInfoData];
            };
            [self presentViewController:vc animated:YES completion:nil];
            
        }
        else {
            [MBProgressHUD showInfoMessage:responseObject[@"des"]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DeLog(@"error:++++%@",error);
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
            [MBProgressHUD showErrorMessage:@"网络不给力，请检查网络"];
        }
        else {
            [MBProgressHUD showErrorMessage:@"服务器繁忙"];
        }
    }];
}


- (void)loadUeserInfoData
{
     BIZUserDetailModel *model = [BIZUserModel shareUser].detailModel;
    
    [SDWebImageTool imageView:self.logoImageView imageName:model.headImg placeHolder:@"事务所图层1" showProgress:NO];
    self.nameLabel.text = model.name;
    self.officeNameLabel.text = model.companyName;
    self.posiationLabel.text = model.position;

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat reOffset = scrollView.contentOffset.y + (App_Frame_Height - 64) * 0.2;
    CGFloat alpha = reOffset / ((App_Frame_Height - 64) * 0.2);
    if (alpha <= 1)//下拉永不显示导航栏
    {
        alpha = 0;
    }
    else//上划前一个导航栏的长度是渐变的
    {
        alpha -= 1;
    }
    // 设置导航条的背景图片 其透明度随  alpha 值 而改变 JSBRGBColor(76, 92, 108)
    UIImage *image = [self imageWithColor:[UIColor colorWithRed:46/255.0 green:140/255.0 blue:229/255.0 alpha:alpha]];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}


/// 使用颜色填充图片
- (UIImage *)imageWithColor:(UIColor *)color
{
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    // 开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 渲染上下文
    CGContextFillRect(context, rect);
    // 从上下文中获取图片
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    // 结束上下文
    UIGraphicsEndImageContext();
    
    return theImage;
}

@end
